package com.neusoft.neuiotms.dcvm.task;

import java.util.Arrays;
import java.util.List;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import com.google.common.collect.Lists;

import lombok.extern.slf4j.Slf4j;

/**
 * IDC 综合运管平台告警管理接口
 *
 * @author sunchf
 */
@Slf4j
@Component
public class IdcAlarmPullTask extends QuartzJobBean {

    @Autowired
    private IdcAlarmPullService idcAlarmPullService;

    @Override
    protected void executeInternal(final JobExecutionContext context) throws JobExecutionException {
        IdcAlarmPullTask.log.info("拉取IDC 综合运管平台告警管理接口开始, {}", System.currentTimeMillis());
        // 机房编码
        List<String> roomCodes = Lists.newArrayList();
        final Object roomCodesObj = context.getJobDetail().getJobDataMap().get("roomCodes");
        if (null != roomCodesObj) {
            final String roomCodesArrStr = (String) roomCodesObj;
            roomCodes = Arrays.asList(roomCodesArrStr.split(","));
        }
        // 告警级别
        List<String> alarmLevel = Lists.newArrayList("一级告警");
        final Object alarmLevelObj = context.getJobDetail().getJobDataMap().get("alarmLevel");
        if (null != alarmLevelObj) {
            final String alarmLevelArrStr = (String) alarmLevelObj;
            alarmLevel = Arrays.asList(alarmLevelArrStr.split(","));
        }
        // 告警类型
        List<String> alarmType = Lists.newArrayList("设备告警");
        final Object alarmTypeObj = context.getJobDetail().getJobDataMap().get("alarmType");
        if (null != alarmTypeObj) {
            final String alarmTypeArrStr = (String) alarmTypeObj;
            alarmType = Arrays.asList(alarmTypeArrStr.split(","));
        }
        // 告警状态
        List<String> alarmStatus = Lists.newArrayList("激活");
        final Object alarmStatusObj = context.getJobDetail().getJobDataMap().get("alarmStatus");
        if (null != alarmStatusObj) {
            final String alarmStatusArrStr = (String) alarmStatusObj;
            alarmStatus = Arrays.asList(alarmStatusArrStr.split(","));
        }
        int size = 100;
        final Object sizeObj = context.getJobDetail().getJobDataMap().get("size");
        if (sizeObj instanceof Integer) {
            size = (int) sizeObj;
        }

        this.idcAlarmPullService.start(roomCodes, alarmLevel, alarmType, alarmStatus, size);
        IdcAlarmPullTask.log.info("拉取IDC 综合运管平台告警管理接口接结束，{}", System.currentTimeMillis());
    }

}
